# Copyright 2014 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# GNU Makefile based on shared rules provided by the Native Client SDK.
# See README.Makefiles for more details.

# In order to build with newlib by default change "pnacl newlib" to
# "newlib pnacl" or use make VAILD_TOOLCHAINS=newlib.
VALID_TOOLCHAINS := pnacl newlib

NACL_SDK_ROOT ?= $(abspath $(CURDIR)/../../..)

# The directory where the NaCl code to test is.
CODE_DIR = ../../unpacker/cpp

include $(NACL_SDK_ROOT)/tools/common.mk

# For NaCl use 'newlib/[Release|Debug]/module.nmf' as pathToNmfFile and
# 'application/x-nacl' as mimeType.
# For PNaCl use 'pnacl/[Release|Debug]/module.nmf' as pathToNmfFile and
# and 'application/x-pnacl' as mimeType.
TEST_PAGE = index.html

# By default CONFIG is Release, but in case of using a rule that contains the
# "debug" string it will be set to Debug. See $NACL_SDK_ROOT/tools/common.mk.
TEST_PAGE_CONFIG = \
  "$(TEST_PAGE)?pathToNmfFile=pnacl/$(CONFIG)/main.nmf&mimeType=application/x-pnacl"

TARGET = main
LIBS = ppapi_simple_cpp nacl_io ppapi_cpp ppapi gtest pthread

CFLAGS = -Wall -Wno-sign-compare -I$(CODE_DIR)
SOURCES = \
  fake_lib_archive.cc \
  fake_volume_reader.cc \
  main.cc \
  $(CODE_DIR)/request.cc \
  request_test.cc \
  $(CODE_DIR)/volume.cc \
  volume_test.cc \
  $(CODE_DIR)/volume_archive_libarchive.cc \
  volume_archive_libarchive_read_test.cc \
  volume_archive_libarchive_test.cc \
  $(CODE_DIR)/volume_reader_javascript_stream.cc \
  volume_reader_javascript_stream_test.cc

# Build rules generated by macros from common.mk:

$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

# The PNaCl workflow uses both an unstripped and finalized/stripped binary.
# On NaCl, only produce a stripped binary for Release configs (not Debug).
ifneq (,$(or $(findstring pnacl,$(TOOLCHAIN)),$(findstring Release,$(CONFIG))))
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

# Run tests. By default it will run the Release tests, but if used as a
# dependency rule to debug_tests_run then it will run the Debug tests.
# In case tests are run for desktop Chrome build, the window is automatically
# closed, but not for Chrome OS build. For Chrome OS build, it has to be closed
# manually.
# -disable-setuid-sandbox is optional, but otherwise it is necessary to set up
# the sandbox for development. See
# https://code.google.com/p/chromium/wiki/LinuxSUIDSandboxDevelopment.
.PHONY: tests_run
tests_run: check_for_chrome all $(TEST_PAGE)
	$(RUN_PY) -C $(CURDIR) -P $(TEST_PAGE_CONFIG) \
	    $(addprefix -E ,$(CHROME_ENV)) -- $(CHROME_PATH_ESCAPE) \
	    $(CHROME_ARGS) --disable-setuid-sandbox \
	    --register-pepper-plugins="$(PPAPI_DEBUG),$(PPAPI_RELEASE)"

# Run Debug tests. Every rule that contains "debug" will build and use the
# debug executables (see #line 113 from $NACL_SDK_ROOT/tools/common.mk with
# findstring).
.PHONY: debug_tests_run
debug_tests_run: tests_run
